import './public-path'
import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/reset.css'
import App from './App.vue'
import routes from './router'
import { renderWithQiankun, qiankunWindow } from 'vite-plugin-qiankun/dist/helper'

let router: any = null
let instance: any = null

function render(props: any = {}) {
  const { container } = props
  const baseUrl = qiankunWindow.__POWERED_BY_QIANKUN__ ? '/app2' : ''
  
  router = createRouter({
    history: createWebHistory(baseUrl),
    routes
  })

  instance = createApp(App)
  instance.use(router)
  instance.use(Antd)
  
  const mountNode = container ? container : document.getElementById('app')
  instance.mount(mountNode)
}

renderWithQiankun({
  mount(props) {
    console.log('app2 mount', props)
    render(props)
  },
  bootstrap() {
    console.log('app2 bootstrap')
  },
  unmount() {
    console.log('app2 unmount')
    instance?.unmount()
    instance = null
    router = null
  },
  update(props) {
    console.log('app2 update', props)
  }
})

if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  render({})
}